package com.sta.staff.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sta.staff.bean.Result;
import com.sta.staff.pojo.Users;
import com.sta.staff.service.UsersService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/backstage/users")
public class UsersController {
    @Autowired
    private UsersService usersService;

    //分页查询所有用户
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        Page<Users> usersPage = usersService.findPage(page, size);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("usersPage", usersPage);
        modelAndView.setViewName("/backstage/users_all");
        return modelAndView;
    }

    //根据id选择需要修改的用户
    @RequestMapping("/edit")
    public ModelAndView findByUId(Integer uid){
        ModelAndView modelAndView = new ModelAndView();
        Users user = usersService.findByUId(uid);
        modelAndView.addObject("user",user);
        modelAndView.setViewName("/backstage/users_edit");
        return modelAndView;
    }
    //根据id修改用户信息
    @RequestMapping("/update")
    public String update(Users users){
        usersService.update(users);
        return "redirect:/backstage/users/all";
    }
    //根据输入模糊或精准查询
    @RequestMapping("/find")
    public ModelAndView findByLike(String search,@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        Page<Users> usersPage = usersService.findByLike(search,page, size);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("usersPage", usersPage);
        modelAndView.setViewName("/backstage/users_all");
        return modelAndView;
    }

    //开除员工
    @RequestMapping("/delete")
    public String delete(Integer uid){
        usersService.delete(uid);
        return "redirect:/backstage/users/all";
    }

    @RequestMapping("/myAll")
    public ModelAndView findMyAll(){
        // 1.获取会话对象
        SecurityContext context = SecurityContextHolder.getContext();
        // 2.获取认证对象
        Authentication authentication = context.getAuthentication();
        // 3.获取登录用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String username = userDetails.getUsername();
        Users user = usersService.findMyself(username);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("/backstage/users_myAll");
        return modelAndView;
    }

    //根据id将自己信息填到修改页面
    @RequestMapping("/myEdit")
    public ModelAndView MyEdit(Integer uid){
        ModelAndView modelAndView = new ModelAndView();
        Users user = usersService.findMyEdit(uid);
        modelAndView.addObject("user",user);
        modelAndView.setViewName("/backstage/users_myEdit");
        return modelAndView;
    }
    //保存修改的个人信息
    @RequestMapping("/myUpdate")
    public ModelAndView MyUpdate(Users users){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",users);
        Result result = usersService.UpdateMyself(users);

        if (!result.isFlag()){
            modelAndView.addObject("message",result.getMessage());
            modelAndView.setViewName("/backstage/users_myEdit");
        }else {
            modelAndView.setViewName("redirect:/backstage/users/myAll");
        }
        return modelAndView;
    }


}
